﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WRJ
{
	public partial class BaseDBUserControl : UserControl
	{
		public BaseDBUserControl()
		{
			InitializeComponent();
			splitContainer1.SplitterDistance = splitContainer1.Width - 70;

			dataGridView1.AllowUserToAddRows = false;
			dataGridView1.AllowUserToDeleteRows = false;
			dataGridView1.AllowUserToOrderColumns = false;
			dataGridView1.AllowUserToResizeColumns = true;
			dataGridView1.AllowUserToResizeRows = false;
			dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;

			dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dataGridView1_DataError);
			dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			dataGridView1.MultiSelect = false;

			label1.ForeColor = Color.Red;
			TipMessage.ForeColor = Color.Blue;
			UpdateTipMessage();

			AddButton.Click += new EventHandler(AddButton_Click);
			AddButton.Click += new EventHandler(UpdateTipMessageEvent);
			DelButton.Click += new EventHandler(DelButton_Click);
			DelButton.Click += new EventHandler(UpdateTipMessageEvent);
			EditButton.Click += new EventHandler(EditButton_Click);
			EditButton.Click += new EventHandler(UpdateTipMessageEvent);
			HelpButton.Click += new EventHandler(HelpButton_Click);
		}

		void UpdateTipMessageEvent(object sender, EventArgs e)
		{
			UpdateTipMessage();
		}

		void UpdateTipMessage()
		{
			string ret;
			ret = "当前模式:\n";
			if (dataGridView1.AllowUserToAddRows)
				ret += "可添加\n";
			else
				ret += "不可添加\n";

			if (dataGridView1.EditMode == DataGridViewEditMode.EditOnF2)
				ret += "可编辑\n";
			else
				ret += "不可编辑\n";

			if (dataGridView1.AllowUserToDeleteRows)
				ret += "可删除";
			else
				ret += "不可删除";
				
			TipMessage.Text = ret;
		}

		void HelpButton_Click(object sender, EventArgs e)
		{
			MessageBox.Show(HelpText, "帮助", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		void EditButton_Click(object sender, EventArgs e)
		{
			if (dataGridView1.EditMode == DataGridViewEditMode.EditProgrammatically)
			{
				dataGridView1.EditMode = DataGridViewEditMode.EditOnF2;
				((Button)sender).Text = "取消编辑";
			}
			else
			{
				dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
				((Button)sender).Text = "编辑";
			}
		}

		void DelButton_Click(object sender, EventArgs e)
		{
			dataGridView1.AllowUserToDeleteRows = !dataGridView1.AllowUserToDeleteRows;
			if (dataGridView1.AllowUserToDeleteRows)
				((Button)sender).Text = "取消删除";
			else
				((Button)sender).Text = "删除";
		}

		void AddButton_Click(object sender, EventArgs e)
		{
			dataGridView1.AllowUserToAddRows = !dataGridView1.AllowUserToAddRows;
			if (dataGridView1.AllowUserToAddRows)
				((Button)sender).Text = "取消添加";
			else
				((Button)sender).Text = "添加";
		}

		void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
		{
			MessageBox.Show(e.Exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			//throw new NotImplementedException();
		}

		string HelpText = @"
1.添加
	单击“添加”按钮可以开启添加模式，此时可以向表格添加数据
	注意添加的数据必须符合数据限制条件，否则提交数据时会出现错误
	单击“取消添加”可以关闭添加模式
	
	1.1 数据限制条件
		用户名的长度不能少于3；
		新增的用户名不能和已经存在的重复；
		日期必须符合“yyyy-mm-dd hh:mm”的格式；
		成绩必须是0-100的整数；
		
2.删除
	和添加模式类似，同样可以开启删除模式和取消删除模式
	在删除模式，按下键盘上面的“Delete”建
	当前选择的数据行会立即被删除

3.修改
	和添加模式类似，同样可以开启编辑模式和取消编辑模式
	在编辑模式，双击需要编辑的单元格，即可编辑数据
	
4.提交
	进行任何添加、删除、修改等操作以后，数据并没有马上提交
	如果确定操作无误，请点击“提交”按钮，如果提示“提交成功”
	则数据已经提交到数据库";
	}
}
